﻿using System;
using System.Configuration;
using System.Reflection;
using SimpleServiceBus.Configuration;
using SimpleServiceBus.Endpoint;
using SimpleServiceBus.Endpoints.DotNetServiceBus.Transport;
using SimpleServiceBus.Utilities.ObjectBuilder;

namespace SimpleServiceBus.Endpoints.DotNetServiceBus.Configuration
{

    public class DotNetServiceBusQueueConfigurationElement : DotNetServiceBusConfigurationElement
    {

        [ConfigurationProperty("queueUri",IsRequired=false)]
        public string QueueUri
        {
            get { return (string)base["queueUri"]; }
            set { base["queueUri"] = value; }
        }

        [ConfigurationProperty("inputQueueName", IsRequired = false)]
        public string InputQueueName
        {
            get { return (string)base["inputQueueName"]; }
            set { base["inputQueueName"] = value; }
        }

        [ConfigurationProperty("poisonQueueName", IsRequired = false)]
        public string PoisonQueueName
        {
            get { return (string)base["poisonQueueName"]; }
            set { base["poisonQueueName"] = value; }
        }
    }

    public class AzureEndpointConfiguration : EndpointConfiguration
    {
        [ConfigurationProperty("azure")]
        public DotNetServiceBusQueueConfigurationElement DotNetServiceBusQueue
        {
            get { return (DotNetServiceBusQueueConfigurationElement)base["azure"]; }
        }


        protected override TTransportType ConfigureTransport<TTransportType>(IEndpointBuilder endpoint)
        {
            var transport = base.ConfigureTransport<TTransportType>(endpoint) as DotNetServiceBusQueueTransport;

            if (transport != null)
            {
                if (!String.IsNullOrEmpty(DotNetServiceBusQueue.QueueUri))
                    transport.QueueUri = new Uri(DotNetServiceBusQueue.QueueUri);
                else if (!String.IsNullOrEmpty(endpoint.Metadata.ReturnAddress))
                    transport.QueueUri = new Uri(endpoint.Metadata.ReturnAddress);

                if (!String.IsNullOrEmpty(DotNetServiceBusQueue.InputQueueName))
                    transport.InputQueueName = DotNetServiceBusQueue.InputQueueName;

                if (!String.IsNullOrEmpty(DotNetServiceBusQueue.PoisonQueueName))
                    transport.PoisonQueueName = DotNetServiceBusQueue.PoisonQueueName;

                if (!String.IsNullOrEmpty(DotNetServiceBusQueue.SolutionName))
                    transport.BusSolutionName = DotNetServiceBusQueue.SolutionName;

                if (!String.IsNullOrEmpty(DotNetServiceBusQueue.SolutionPassword))
                    transport.BusSolutionPassword = DotNetServiceBusQueue.SolutionPassword;

                if (DotNetServiceBusQueue.SendTimeOut != default(TimeSpan))
                    transport.SendTimeOut = DotNetServiceBusQueue.SendTimeOut;

                if (DotNetServiceBusQueue.ReceiveTimeOut != default(TimeSpan))
                    transport.ReceiveTimeOut = DotNetServiceBusQueue.ReceiveTimeOut;

                
            }

            return transport as TTransportType;
        }

        public static IMessagingEndpoint CreateConfiguredEndpoint(params Assembly[] messageHandlerAssemblies)
        {
            return CreateConfiguredEndpoint(null,messageHandlerAssemblies);
        }

        public static IMessagingEndpoint CreateConfiguredEndpoint(IObjectBuilder ioC,params Assembly[] messageHandlerAssemblies)
        {
            return CreateConfiguredEndpoint<DotNetServiceBusEndpointBuilder<MessagingEndpoint>, DotNetServiceBusQueueTransport>(ioC,messageHandlerAssemblies);
            
        }

        public static IEndpointBuilder ConfigureEndpoint()
        {
            return ConfigureEndpoint(null);
        }

        public static IEndpointBuilder ConfigureEndpoint(IObjectBuilder ioC)
        {
            return ConfigureEndpoint<DotNetServiceBusEndpointBuilder<MessagingEndpoint>, DotNetServiceBusQueueTransport>();
        }
    }
}